//
//  ZDTabBarController.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/10/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDTabBarController.h"
#import "ZDHomeViewController.h"
#import "ZDMessageViewController.h"
#import "ZDDiscoveryViewController.h"
#import "ZDProfileViewController.h"
#import "ZDTabBar.h"
#import "UIView+ZUtility.h"
#import "ZDComposeViewController.h"

@interface ZDTabBarController ()

@property (nonatomic, strong) UIButton *composeButton;

@end

@implementation ZDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
/*
    //使用kvc替换系统自带tabBar  ———— 因为系统自带tabBar是只读属性
    [self setValue:[[ZDTabBar alloc] init] forKey:@"tabBar"];
    
    [self addChildViewController:[[ZDHomeViewController alloc] init] title:@"首页" normalImageName:@"tabbar_home" selectedImageName:@"tabbar_home_highlighted"];
    [self addChildViewController:[[ZDMessageViewController alloc] init] title:@"消息" normalImageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_highlighted"];
    [self addChildViewController:[[ZDDiscoveryViewController alloc] init] title:@"发现" normalImageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_highlighted"];
    [self addChildViewController:[[ZDProfileViewController alloc] init] title:@"我" normalImageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_highlighted"];
 */
    
    //设置tabBar的tintColor属性  可以将tabBar中的图片和文本都渲染成指定颜色
    self.tabBar.tintColor = [UIColor orangeColor];
    
    [self addChildViewController:[[ZDHomeViewController alloc] init] title:@"首页" normalImageName:@"tabbar_home"];
    [self addChildViewController:[[ZDMessageViewController alloc] init] title:@"消息" normalImageName:@"tabbar_message_center"];
    //添加一个占位控制器 目的是在tabBar中占位 为中间按钮腾出空间
    [self addChildViewController:[[UIViewController alloc] init] title:@"" normalImageName:@""];
    
    [self addChildViewController:[[ZDDiscoveryViewController alloc] init] title:@"发现" normalImageName:@"tabbar_discover"];
    [self addChildViewController:[[ZDProfileViewController alloc] init] title:@"我" normalImageName:@"tabbar_profile"];
    
    //viewDidLoad中 添加按钮到tabBar中时，通过查看view的层次结构发现，button是在tabBar的最底层，没法响应点击事件(事件被tabBar中间的item拦截) ———— 希望将button放在tabBar的最顶层，响应事件，所以在viewWillAppear中添加中间按钮
//    CGFloat width = self.tabBar.width / 5;
//    CGFloat height = self.tabBar.height;
//    self.composeButton.frame = CGRectMake(2 * width, 0, width, height);
}


//添加中间按钮
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    CGFloat width = self.tabBar.width / 5;
    CGFloat height = self.tabBar.height;
    self.composeButton.frame = CGRectMake(2 * width, 0, width, height);
}


/**
 *  添加子控制器
 *
 *  @param childController   子控制器
 *  @param title             子控制器标题
 *  @param normalImageName   tabBarItem normal图片
 *  @param selectedImageName tabBarItem selected图片
 */
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName
{
    childController.title = title;
    
    //设置tabBarItem的选中状态的文本颜色
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
    [childController.tabBarItem setImage:[UIImage imageNamed:normalImageName]];
    
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    
    //设置图片的渲染模式 ———— 默认是被tabBarItem渲染成蓝色
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [childController.tabBarItem setSelectedImage:selectedImage];
    
    [self addChildViewController:[[UINavigationController alloc] initWithRootViewController:childController]];
}


/**
 *  添加子控制器
 *
 *  @param childController   子控制器
 *  @param title             子控制器标题
 *  @param normalImageName   tabBarItem normal图片
 */
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title normalImageName:(NSString *)normalImageName
{
    childController.title = title;
    
    if (normalImageName.length != 0)
    {
        [childController.tabBarItem setImage:[UIImage imageNamed:normalImageName]];
    }
    
    [self addChildViewController:[[UINavigationController alloc] initWithRootViewController:childController]];
}


#pragma mark - 中间按钮点击回调方法
- (void)composeButtonClick
{
//    ZDLog(@"%s", __func__);
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ZDComposeViewController alloc] init]];
    
    [self presentViewController:nav animated:YES completion:nil];
}



#pragma mark 懒加载
- (UIButton *)composeButton
{
    if (!_composeButton)
    {
        _composeButton = [[UIButton alloc] init];
        
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        //绑定事件
        [_composeButton addTarget:self action:@selector(composeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self.tabBar addSubview:_composeButton];   //如果在layoutSubViews中添加 会执行多次 影响性能
    }
    
    return _composeButton;
}



@end
